#include "stdafx.h"
#include "Material.h"

#include "LoadTexture.h"

#include "StringUtil.h"

using namespace mgfx;

Material::ResourceMap Material::mResources;

Material* Material::create(const std::string & name)
{
	std::string matName = name;
	if(matName == "")
	{
		matName = "MatAuto" + std::string(mutil::toString(mNextAutoId++));
	}

	Material * mat = new Material;
	mResources[matName] = mat;
	return mat;
}

void Material::freeAll()
{
	ResourceMap::iterator itr, end = mResources.end();
	for(itr = mResources.begin() ; itr != end ; ++itr)
	{
		delete itr->second;
	}
}

Material::Material()
{
	// Should be Renderer::TEXTURE_NONE, but need to include Renderer.h
	DiffuseMap = -1;
	NormalMap = -1;
	SpecularMap = -1;
}

void Material::createDefault()
{
	Default = Material::create("MatDefault");
	Default->initDefault();
}

Material * Material::Default = NULL;
uint Material::mNextAutoId = 0;

void Material::initDefault()
{
	DiffuseColor = Color::WHITE;
	SpecularIntensityAndPower = XMFLOAT2(0.8f, 128.0f);
	DiffuseMap = loadTextureFromFile("ground_diffuse.jpg");
	NormalMap = loadTextureFromFile("ground_normal.jpg");;
	SpecularMap = loadTextureFromFile("ground_specular.jpg");;
}
